{extend name="index/index" /}
{block name="body"}
<body data-type="widgets">
{/block}
{block name="main"}
        <div class="row-content am-cf">
                <div class="row">
                    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                        <div class="widget am-cf">
                            <div class="widget-head am-cf">
                                <div class="widget-title  am-cf">角色列表</div>


                            </div>
                            <div class="widget-body  am-fr">

                                <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                                    <div class="am-form-group">
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <button type="button" class="am-btn am-btn-default am-btn-success"
                                                    id="add-role" url="{:url('/admin/role/create')}"><span
                                                    class="am-icon-plus"></span> 新增
                                            </button>

                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <form action="{:url('admin/role/search')}" method="post">
                                    <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
                                        <div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
                                            <input type="text" class="am-form-field " name="like" placeholder="请输入要查询的角色名">
                                            <span class="am-input-group-btn">
                    <input type="submit" class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search">
                  </span>
                                        </div>
                                    </div>
                                </form>

                                <div class="am-u-sm-12">
                                    <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black " id="example-r">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>角色</th>
                                                <th>创建时间</th>
                                                <th style="text-align: center">状态</th>
                                                <th style="text-align: center">操作</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {volist name="data" id="v"}
                                            <tr class="gradeX">
                                                <td>{$v['id']}</td>
                                                <td>{$v['role']}</td>
                                                <td>{$v['create_time']}</td>
                                                <td>
                                            <div class="am-u-sm-9">
                                                <div class="tpl-switch">
                                                    <input type="checkbox" class="ios-switch bigswitch tpl-switch-btn status-ajax" roleId="{$v['id']}" url="{:url('admin/role/update','id='.$v['id'])}" name="status"
                                                           {:$v['status']==1?'checked':'' }>
                                                    <div class="tpl-switch-btn-view">
                                                        <div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </td>
                                                <td style="text-align: center">
                                                    <div class="tpl-table-black-operation">
                                                           <a class="edit-role" url="{:url('admin/role/edit','id='.$v['id'])}">
                                                    <i class="am-icon-pencil"></i> 编辑/权限
                                                </a>
                                                        <a href="javascript:dodel({$v['id']},'{:url('admin/role/delete','id='.$v['id'])}')" class="tpl-table-black-operation-del" >
                                                    <i class="am-icon-trash"></i> 删除
                                                </a>
                                                    </div>
                                                </td>
                                            </tr>
                                            {/volist}


                                            <!-- more data -->
                                        </tbody>
                                    </table>
                                </div>
                                <div class="am-u-lg-12 am-cf">

                                    <div class="am-fr">
                                        <ul class="am-pagination tpl-pagination">
                                            <li class=""><a href="{:url('admin/role/index',['page'=>($page-1)])}">«</a></li>
                                            {for start="1" end="$count+1"}
                                            <li class="{:$page==$i?'am-active':''}"><a href="{:url('admin/role/index',['page'=>$i])}">{$i}</a></li>
                                            {/for}
                                            <li><a href="{:url('admin/role/index',['page'=>($page+1)])}">»</a></li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
{/block}
{block name="Modals"}
<div style="margin:100px auto;/*padding:30px;*/width:860px;/*broder:1px solid #ccc;border-radius:16px;background:#ffe;*/"
     id="Modals-frame">
</div>
<span id="js-ajax"></span>

<!-- /role/index 基础调用JS -->
<script>
$(function(){

    // 点亮左侧对应导航
    (function(){
            $('.sidebar-nav-link > a').removeClass("active");
            $('#administrate').addClass("active");
            $('.sidebar-nav-sub > li > a').removeClass("sub-active");
            $('#administrate-role').addClass("sub-active");
    })();

    //JS原生阻止模态框内点击事件冒泡
    document.getElementById('Modals-frame').addEventListener('click', function (e) {
        e.stopPropagation()
    }, false);
    //点击模态框外围隐藏模态框
    Modals.onclick = function () {
        this.style.display = 'none';
    }
     //更改状态
    $(".status-ajax").change(function() { 
        // console.log($(this).prop("checked"));
        var str;
        if($(this).prop("checked")){
            str = 'status=1&source=status';
        }else{
            str = 'status=0&source=status';
        }
        var ls = $(this);
        $.ajax({
            type: 'put',
            data: str,
            url: $(this).attr('url'),
            success: function (data) {
                // console.log(str);
                if (data == 0) {
                    toastr.error("更改失败，请重试");
                    if(ls.prop("checked")){
                        ls.removeAttr("checked");
                    }else{
                        ls.attr("checked");
                    }
                } else {
                    toastr.success("更改成功");
                }
            },
            error: function () {
                toastr.error("出现错误，请重试");
                if(ls.prop("checked")){
                    ls.removeAttr("checked");
                }else{
                    ls.attr("checked");
                }
            }
        });
    });
    //删除操作
    dodel = function (id,url){
        var s = confirm("你确信要删除"+id+"号角色？");
        if(s){
            $.ajax({
                type: 'delete',
                url: url,
                success: function (data) {
                    if (data == 0) {
                        toastr.error("删除失败，请重试");
                    } else {
                        toastr.success("删除成功");
                    }
                        location = location;
                },
                error: function () {
                    toastr.error("出现错误，请重试");
                }
            });
        }
    }

    //获取增加模态框并展示
    $('#add-role').click(function () {
        $.ajax({
            type: 'get',
            url: $(this).attr('url'),
            success: function (data) {
                // console.log(data);
                $('#Modals-frame').html(data);
                $('#Modals').css('display', 'block');
                // $('#Modals-add').css('display', 'block');
            },
            error: function () {
                toastr.error("出现错误，请重试");
            }
        });
    });

    //获取编辑模态框并展示
    $('.edit-role').click(function () {
        // console.log($(this));
        // return ;
        $.ajax({
            type: 'get',
            url: $(this).attr('url'),
            success: function (data) {
                // console.log(data);
                $('#Modals-frame').html(data);
                $('#Modals').css('display', 'block');
                // $('#Modals-add').css('display', 'block');
            },
            error: function () {
                toastr.error("出现错误，请重试");
            }
        });
    });

}); //结束JQ包裹

</script>
{/block}